<template>
  <div class="room-content">
    <Navbar appTitle="物料需求明细" />
    <van-dropdown-menu active-color="#1989fa">
      <van-dropdown-item @change='searchQuery' v-model="dropdownObj.sortType" :options="SelectList" />
      <van-dropdown-item
        title="使用日期"
        @open="createdTimeBeginVisible = true"
      />
    </van-dropdown-menu>
    <!--分页列表 -->
    <div class="listBox" :style="{ height: GLOBAL.currentEnvironment ? 'calc(100vh - 0.5rem)' : 'calc(100vh - 1rem)' }">
      <pageList ref="pageList" :checkedShow="false" :columns="columns" :listUrl="listUrl">
        <div slot="body" slot-scope="{ item }">
          <div class="rt-column">
            <div class="rt-row">
              <div class="column">
                <span class="column-t">使用时间：</span>
                <span class="column-c" :style="{color: setColor(item.useDate)}"
                  >{{ item.useDate || '--' }}</span
                >
              </div>
              <div class="column">
                <span class="column-t">需求数量：</span>
                <span class="column-c">{{ item.totalNumText || '--' }}</span>
              </div>
            </div>
          </div>
        </div>
      </pageList>
    </div>
    <!--使用开始时间 -->
    <van-popup v-model="createdTimeBeginVisible" position="bottom">
      <van-datetime-picker
        @confirm="confirmCreatedStartTime"
        @cancel="cancelCreatedStartTime"
        type="date"
        title="提交开始时间"
        v-model="minDate"
      />
    </van-popup>
    <!--使用结束时间 -->
    <van-popup v-model="createdTimeEndVisible" position="bottom">
      <van-datetime-picker
        @confirm="confirmCreatedEndTime"
        @cancel="cancelCreatedEndTime"
        type="date"
        title="提交结束时间"
        :min-date="createEndMinDate"
      />
    </van-popup>
  </div>
</template>

<script>
import Navbar from "@/components/navBar/navBar_right";
import TabBar from "@/components/TabBar";
import pageList from "@/components/pageList";
import moment from 'moment'
export default {
  components: {
    Navbar,
    TabBar,
    pageList,
  },
  data() {
    return {
      listUrl: '', //列表接口
      columns: {
        titleKey: "code", //标题
      },
      dropdownObj: {
        sortType: 'ASC'
      },
      // 日期筛选
      createdTimeBeginVisible: false,
      createdTimeEndVisible: false,
      minDate: new Date(),
      createEndMinDate: new Date(),
      createdTimeRead: "",
      // 日期筛选结束
      SelectList: [
        { text: '按使用时间正序排列', value: 'ASC' },
        { text: '按使用时间倒序排列', value: 'DESC' },
      ]
    };
  },
  computed: {},
  created() {
    if (this.$route.query.goodsId) {
      this.listUrl = `/planmgt/erpProductMaterialRequirement/infoList?goodsId=${this.$route.query.goodsId}`
    }
  },
  mounted() {
    this.searchQuery();
  },
  methods: {
    // 选择提交开始时间
    confirmCreatedStartTime(date) {
      this.createEndMinDate = date;
      this.dropdownObj.useDateBegin = moment(date)
        .startOf("day")
        .format("YYYY-MM-DD HH:mm:ss");
      this.dropdownObj.useDateEnd = ""; // 切换开始时间后直接置空结束时间重新选择
      this.createdTimeBeginVisible = false;
      this.createdTimeEndVisible = true;
      this.createdTimeRead = moment(date).format("YYYY-MM-DD");
    },
    // 取消提交开始时间
    cancelCreatedStartTime() {
      this.dropdownObj.useDateBegin = "";
      this.dropdownObj.useDateEnd = "";
      this.createdTimeRead = "";
      this.minDate = new Date();
      this.searchQuery();
      this.createdTimeBeginVisible = false;
    },
    // 选择提交结束时间
    confirmCreatedEndTime(date) {
      this.dropdownObj.useDateEnd = moment(date)
        .endOf("day")
        .format("YYYY-MM-DD HH:mm:ss");
      this.createdTimeEndVisible = false;
      this.createdTimeRead =
        this.createdTimeRead + "~" + moment(date).format("YYYY-MM-DD");
      this.searchQuery();
    },
    // 取消提交结束时间
    cancelCreatedEndTime() {
      this.dropdownObj.useDateBegin = "";
      this.dropdownObj.useDateEnd = "";
      this.createdTimeRead = "";
      this.minDate = new Date();
      this.searchQuery();
      this.createdTimeEndVisible = false;
    },
    setColor(d) {
      if (d) {
        return moment(d).isBefore(moment()) ? '#ff4d4f' : ''
      } else {
        return ''
      }
    },
    // 搜索
    searchQuery() {
      let params = {
        ...this.dropdownObj,
        sortColumn: 'useDate'
      };
      this.$refs.pageList.searchQuery(params);
    },
  },
};
</script>
<style lang="scss" scoped>
.room-content {
  height: 100vh;
  .serach {
    height: 0.33rem;
    background: #f2f2f6;
    border-radius: 0rem;
    margin-bottom: 0.16rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    line-height: 0.33rem;
    padding: 0.1rem 0.12rem;
    // border: 1px solid red;
    .placeh {
      font-size: 0.16rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #888888;
      line-height: 0.23rem;
    }
    .icon {
      width: 0.15rem;
      height: 0.16rem;
    }
  }
  .serach-content {
    // height: 1.52rem;
    background: #ffffff;
    padding: 0 0.16rem 0.16rem 0.16rem;
    .content {
      display: flex;
      .tapbtn {
        // height: 0.23rem;
        // background: #1890ff;
        background: #f2f2f6;
        border-radius: 0.02rem;
        font-size: 0.16rem;
        font-family: PingFang SC;
        font-weight: 500;
        color: #888888;
        line-height: 0.23rem;
        margin-right: 0.12rem;
        text-align: center;
        padding: 0.04rem 0.28rem;
        cursor: pointer;
      }
      .active {
        color: #ffffff;
        background: #1890ff;
      }
    }
  }
}
.labelBox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding-left: 0.14rem;
  .labelLeft {
    display: flex;
    justify-content: center;
    align-items: center;
    background: #f0f6fe;
    border-radius: 0.02rem;
    box-sizing: border-box;
    padding: 0.03rem 0.07rem;
    margin-right: 0.05rem;
    font-size: 0.11rem;
  }
  .labelRight {
    display: flex;
    justify-content: center;
    align-items: center;
    background: #f0f6fe;
    border-radius: 0.02rem;
    box-sizing: border-box;
    padding: 0.03rem 0.07rem;
    font-size: 0.11rem;
  }
}
.listBox {
  height: calc(100vh - 1.5rem);
  overflow-y: auto;
}
.dialogContentBox {
  text-align: left;
  margin: 0.1rem 0.19rem;
  color: #191f25;
  font-size: 0.16rem;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
/deep/ .van-dropdown-menu__bar {
  box-shadow: none;
}
.operateBox {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  box-sizing: border-box;
  padding-right: 0.07rem;
  font-size: 0.14rem;
  font-weight: 500;
  color: #1890ff;
}
.rt-column {
  padding: 0.17rem 0 0 0.17rem;
  .arrow {
    color: #1890ff;
    // float: right;
    // margin-right: -0.05rem;
    // display: block;
    position: absolute;
    right: 0.15rem;
  }
  //  border: 1px solid red;
}
.rt-row {
  margin-bottom: 0.1rem;
  .column {
    width: 100%;
    .column-t {
      font-size: 0.14rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #888888;
      margin-bottom: 0.03rem;
    }
    .column-c {
      font-size: 0.14rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: #191f25;
    }
  }
}
</style>